package com.dfe.backend.controller;

import com.dfe.backend.common.bean.real.circuit.SpdCircuit;
import com.dfe.backend.common.bean.virtual.VirtualLink;
import com.dfe.backend.common.bean.virtual.VirtualNode;
import com.dfe.backend.common.bean.virtual.VirtualReference;
import com.dfe.backend.common.cache.CaffeineCache;
import com.dfe.backend.common.sax.spd.SpdData;
import com.dfe.backend.data.RtnData;
import com.dfe.backend.dto.RealCircuitDTO;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【描述】：回路分析控制器
 *
 * @author LiChang
 * @since 2025-8-4 14:58
 */
@Slf4j
@RestController
@RequestMapping("/circuit")
@RequiredArgsConstructor
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class CircuitController {

    CaffeineCache caffeineCache;

//    WebSocketClient webSocketClient;


    /**
     * 【描述】: 获取IED信息列表
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @GetMapping("/getIedInfoList")
    public RtnData getIedInfoList() {
        return RtnData.ok(caffeineCache.iedInfoListCache().getIfPresent("scd"));
    }

    /**
     * 【描述】: 根据IED获取虚拟节点列表
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @GetMapping("/getVirtualNodeList")
    public RtnData getVirtualNodeList(@RequestParam String iedName) {
        // 获取虚拟节点列表
        List<VirtualNode> virtualNodeList = caffeineCache.iedVirtualNodeCache().getIfPresent(iedName);
        if (virtualNodeList != null) {
            for (VirtualNode virtualNode : virtualNodeList) {
                for (VirtualLink virtualLink : virtualNode.getLinks()) {
                    // 获取连接路径
                    VirtualReference virtualReference = caffeineCache.virtualReferenceCache().getIfPresent(virtualLink.getNum());
                    if (virtualReference != null) {
                        virtualLink.setValue(virtualReference.getValue());
                        virtualLink.setStatus(virtualReference.getStatus());
                    }
                }
            }
        }
        return RtnData.ok(virtualNodeList);
    }

    /**
     * 【描述】: 根据IED获取外部连接列表
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @GetMapping("/getExtRefList")
    public RtnData getExtRefList(@RequestParam String iedName) {
        return RtnData.ok(caffeineCache.iedExtRefCache().getIfPresent(iedName));
    }

    /**
     * 【描述】: 根据IED获取外部连接列表
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @PostMapping("/test")
    public RtnData test() {
        SpdData spdData = caffeineCache.spdDataCache().getIfPresent("spd");
        System.out.println("执行完毕");
        return RtnData.ok(spdData);
    }


    /**
     * 【描述】: 获取全站设备拓扑
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @GetMapping("/getAllDeviceData")
    public RtnData getAllDeviceData() {
        // 获取SPD信息
        SpdData spdData = caffeineCache.spdDataCache().getIfPresent("spd");
        if (spdData != null) {
            return RtnData.ok(spdData.getDeviceData());
        }
        return RtnData.fail("SPD分析信息不存在！");
    }


    /**
     * 【描述】: 获取portInfo
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @GetMapping("/getRealPort")
    public RtnData getRealPort(@RequestParam String key) {
        // 获取端口信息
        return RtnData.ok(caffeineCache.realPortCache().getIfPresent(key));
    }

    /**
     * 【描述】: 获取对应的真实路径
     *
     * @param realCircuitDTO realCircuitDTO
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-10-13 11:15
     */
    @PostMapping("/getRealCircuit")
    public RtnData getRealCircuit(@RequestBody RealCircuitDTO realCircuitDTO) {
        SpdData spdData = caffeineCache.spdDataCache().getIfPresent("spd");
        if (spdData != null) {
            if (RealCircuitDTO.isEmpty(realCircuitDTO)) {
                return RtnData.ok(spdData.getSpdCircuitMap());
            } else {
                String sendIedName = realCircuitDTO.getSendIedName();
                String receiveIedName = realCircuitDTO.getReceiveIedName();
                // 获取端口号
                String port = caffeineCache.realPortCache().getIfPresent(sendIedName + "." + receiveIedName + "." + realCircuitDTO.getAppId());
                List<SpdCircuit> spdCircuitList = spdData.getSpdCircuitMap().get(sendIedName + ":" + receiveIedName + "." + port);
                return RtnData.ok(spdCircuitList);
            }
        }
        return null;
    }

}
